import {
  imageUrl
} from "../../utils/imageutil.js"
const app = getApp();
Page({
  data: {
    imageUrl: imageUrl,
    navcurr: '',
    search: '',
    page: 1,
    flag: true,
    type: '',
    timeData: {},

    navlist: [{
        title: '全部',
        id: ""
      }, {
        title: '待付款',
        id: 1,
      }, {
        title: '已支付',
        id: 2
      },
      // {
      //   title: '待购保险',
      //   id: 12
      // }, 
      {
        title: '已接单',
        id: 3
      }, {
        title: '待评价',
        id: 4
      }, {
        title: '已完成',
        id: 5
      }
      , {
        title: '退款',
        id: 6
      }
    ],
    list: [],

    paytype_list: [],
    has_jump: false,
    show_code_img: false,

  },

  get_paytype(storeid) {
    return new Promise((a, b) => {
      getApp().globalData.post({
        url: '/api/applet.ouyoung/paytype.html',
        data: {
          storeid
        }
      }, true).then(res => {
        if (res.data.code == 1) {
          this.setData({
            paytype_list: res.data.data
          })
          a()
        } else {
          getApp().showModal(res.data.msg)
        }
      })
    })
  },
  applyRefundTcOrder(e) {
    if (e.currentTarget.dataset.refund > 0) {
      return
    }
    getApp().globalData.posts('/api/ouyoung/order/applyRefundTcOrder', {
      order_number: e.currentTarget.dataset.order,
    }).then(res => {
      getApp().showModal(res.data.msg).then(() => {
        this.setData({
          page: 1,
          list: []
        })
        this.getList()
      })
    })
  },
  changernavcurr(e) { //订单顶部状态切换
    this.setData({
      navcurr: e.currentTarget.dataset.id,
      type: e.currentTarget.dataset.id,
      page: 1,
      list: [],
      flag: true
    })
    this.getList()
  },
  linend(e) {
    let list = this.data.list;
    list.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      list
    })
    this.getDown(e.currentTarget.dataset.id)
  },
  getDown(id) {
    getApp().globalData.post({
      url: "/api/applet.oyc/ay_update_state.html",
      data: {
        id: id
      }
    }, true).then(res => {
      console.log(res);
    })
  },
  // onChange(e) {
  //   let aa =this.data.list;
  //   const bb = {...this.data.list[e.currentTarget.dataset.index],...{m:e.detail.minutes,d:e.detail.milliseconds}}
  //   console.log(bb);
  //   // console.log(bb);
  //   this.setData({
  //     [`list[${e.currentTarget.dataset.index}]`]: bb
  //   })
  // },
  change_search(e) {
    this.setData({
      search: e.detail.value,
      page: 1,
      flag: true,
      list: [],
    })
    this.getList()
  },
  onLoad: function (options) {
    if (options.navcurr) {
      this.setData({
        navcurr: options.navcurr,
        type: Number(options.navcurr) 
      })
    }
    wx.getSystemInfo({
      success: (res) => {
        console.log(res.windowHeight);
        const aa = res.windowWidth;
        const cc = res.windowHeight - aa / 750 * (84 + 84);
        this.setData({
          scroll: cc
        })
      }
    })

  },
  scrolltolower() {
    this.getList();
  },
  refresherrefresh() {
    this.setData({
      topflag: true,
      page: 1,
      list: [],
      flag: true
    })
    this.getList();

  },
  onShow() {

    if (this.data.has_jump) {
      getApp().showModal('是否已完成支付', true).then(() => {
        this.setData({
          search: '',
          page: 1,
          flag: true,
          navcurr: 1,
          list: [],
        })
        this.getList()
      })
      this.setData({
        has_jump: false
      })
    }

    this.setData({
      search: '',
      page: 1,
      flag: true,
      list: [],
    })
    this.getList()
  },
  onPullDownRefresh() {
    this.setData({
      search: '',
      page: 1,
      flag: true,
      navcurr: 1,
      type: '',
      list: [],
    })
    this.getList()
  },
  onReachBottom() {
    this.getList()
  },
  to_Cancel(e) { //取消订单
    wx.navigateTo({
      url: '../matron/abrogate?wid=' + e.currentTarget.dataset.id + '&type=1',
    })
  },
  search(item) {
    wx.setStorageSync('buy_bx', item.currentTarget.dataset.item)
    wx.navigateTo({
      url: '/insurance/index?order_type=' + true + '&item=' + true,
    })

  },

  pay(e) { //微信支付
    this.get_paytype(e.currentTarget.dataset.store_id).then(() => {
      let paytype_list = this.data.paytype_list
      let itemList = []
      paytype_list.forEach((v, i) => {
        itemList.push(v.title)
      })
      wx.showActionSheet({
        itemList,
        success: (res) => {
          let tapIndex = res.tapIndex
          let paytype = paytype_list[tapIndex].paytype

          let _this = this;
          if (paytype == 6) {
            wx.setStorageSync('pay_info', {
              do: 'pay',
              order_id: e.currentTarget.dataset.id,
              user_id: wx.getStorageSync('userInfo').id,
              openid: wx.getStorageSync('userInfo').openid,
              type: 4,
              paytype
            })
            wx.navigateTo({
              url: '/online_bank/list?id=' + e.currentTarget.dataset.id
            })
            return
          }
          app.globalData.post({
            data: {
              do: 'pay',
              order_id: e.currentTarget.dataset.id,
              user_id: wx.getStorageSync('userInfo').id,
              openid: wx.getStorageSync('userInfo').openid,
              type: 4,
              paytype
            }
          }).then(res => {
            let data = res.data
            if (data.pay_status == 2) {
              wx.showModal({
                title: '提示',
                content: '支付成功',
                showCancel: false,
                success: () => {
                  _this.setData({
                    navcurr: 1,
                    list: [],
                  })
                  _this.getList()
                }
              })
              return
            }
            if (data.jump) {
              this.selectComponent('#zfb_pay').get_code(data.jump)
              this.setData({
                show_code_img: true,
                has_jump: true,
              })
              return
            } else {
              this.setData({
                has_jump: false
              })
            }
            if (getApp().globalData.appletpayves_info.work == 3) {
              getApp().payPlugin(data.orderNo).then(() => {
                getApp().showModal('支付成功').then(() => {
                  _this.setData({
                    navcurr: 1,
                    list: [],
                  })
                  _this.getList()
                })
              })
              return
            }
            if (!data.package) {
              getApp().showModal('暂无法使用该支付方式，请选择其他支付方式', true).then(() => {
                this.pay(e)
              })
              return
            }
            wx.requestPayment({
              nonceStr: data.nonceStr,
              package: data.package,
              paySign: data.paySign,
              timeStamp: data.timeStamp,
              signType: data.signType,
              complete(s) {
                if ("requestPayment:ok" == s.errMsg) {
                  wx.showToast({
                    title: "支付成功"
                  })
                  _this.setData({
                    navcurr: 1,
                    list: [],
                  })
                  _this.getList()
                }
              }
            })
          })
        }
      })
    })
  },
  getList() { //订单列表
    if (!this.data.flag) {
      return
    }
    app.globalData.post({
      url: "/api/applet.oyc/fx_order_list",
      data: {
        state: this.data.type,
        page: this.data.page,
        // pagesize: 10,
        like: this.data.search,
        uid: wx.getStorageSync('userInfo').id
      }

    }, true).then(res => {
      console.log(res);
      if (res.data.data.length == 0) {
        this.setData({
          flag: false,
          topflag: false
        })
      } else {
        res.data.data.forEach((item, index) => {
          if (item.state == 10) {
            const cc = Number(+new Date(item.addtime) / 1000);
            const ee = Number(parseInt(+new Date() / 1000));
            const dd = parseInt(1800 - (ee - cc));
            item.dao = dd;                                                                                               
          }
        })
        let arr = this.data.list.concat(res.data.data);
        this.setData({
          list: arr,
          page: this.data.page + 1,
          topflag: false
        })
      }
    })
  },
  // getList() { //订单列表
  //   if (!this.data.flag) {
  //     return
  //   }
  //   app.globalData.post({
  //     data: {
  //       do: 'ReleaseWorkLlist',
  //       type: this.data.type,
  //       page: this.data.page,
  //       pagesize: 10,
  //       search: this.data.search,
  //       uid: wx.getStorageSync('userInfo').id
  //     }
  //   }).then(res => {
  //     console.log(res);
  //     if (res.data.data.length == 0) {
  //       this.setData({
  //         flag: false,
  //         topflag: false
  //       })
  //     } else {
  //       res.data.data.forEach((item, index) => {
  //         if (item.state == 10) {
  //           const cc = Number(+new Date(item.addtime) / 1000);
  //           const ee = Number(parseInt(+new Date() / 1000));
  //           const dd = parseInt(1800 - (ee - cc));
  //           item.dao = dd;
  //         }
  //       })
  //       let arr = this.data.list.concat(res.data.data);
  //       this.setData({
  //         list: arr,
  //         page: this.data.page + 1,
  //         topflag: false
  //       })
  //     }
  //   })
  // },

  // getList() { //订单列表
  //     if (!this.data.flag) {
  //         return
  //     }
  //     app.globalData.post({
  //         data: {
  //             do: 'ReleaseWorkLlist',
  //             type: this.data.type,
  //             page: this.data.page,
  //             pagesize: 10,
  //             search: this.data.search,
  //             uid: wx.getStorageSync('userInfo').id
  //         }
  //     }).then(res => {
  //       console.log(res);
  //         if (res.data.data.length == 0) {
  //             this.setData({
  //                 flag: false
  //             })
  //         } else {
  //             // res.data.data.forEach((item,index)=>{
  //             //   if(item.state == 10){
  //             //     const cc =Number(+new Date(item.addtime)/1000);
  //             //     const ee =Number(parseInt(+new Date(item.addtime)/1000));
  //             //     const bb = 30*60;
  //             //     const gg =ee+bb;
  //             //     const dd = parseInt(gg-cc);

  //             //     if(index==0){
  //             //       console.log(parseInt(dd));
  //             //     }


  //             //     item.dao=parseInt(dd) 

  //             //   }
  //             // })
  //             let arr = this.data.list.concat(res.data.data);
  //             this.setData({
  //                 list: arr,
  //                 page: this.data.page + 1
  //             })
  //         }
  //     })
  // },
  to_invoice() {
    wx.navigateTo({
      url: '/invoice/index',
    })
  },
  to_plan(e) { //订单详情

    let title='order_id'
    if(e.currentTarget.dataset.type==1){
      title="wid"
    }else{
      title='order_id'
    }
    // let title='wid'
    wx.navigateTo({
      url: '../matron/plan?'+title+'='+ e.currentTarget.dataset.id,
    })
  },
  to_as_add(e) {
    let is_after_sale = e.currentTarget.dataset.is_after_sale;
    if (is_after_sale == 1) {
      return
    }
    wx.navigateTo({
      url: './as_add?id=' + e.currentTarget.dataset.id,
    })
  },
  RenewWork(e) {
    this.get_paytype(e.currentTarget.dataset.store_id).then(() => {
      let paytype_list = this.data.paytype_list
      let itemList = []
      paytype_list.forEach((v, i) => {
        itemList.push(v.title)
      })
      wx.showActionSheet({
        itemList,
        success: (res) => {
          let tapIndex = res.tapIndex
          let paytype = paytype_list[tapIndex].paytype

          let wid = e.currentTarget.dataset.id;
          if (paytype == 6) {
            wx.setStorageSync('pay_info', {
              a: 'wxapp2',
              do: 'RenewWork',
              uid: wx.getStorageSync('userInfo').id,
              work_id: wid,
              paytype
            })
            wx.navigateTo({
              url: '/online_bank/list?id=' + wid
            })
            return
          }
          app.globalData.post({
            data: {
              a: 'wxapp2',
              do: 'RenewWork',
              uid: wx.getStorageSync('userInfo').id,
              work_id: wid,
              paytype
            }
          }).then(res => {
            let data = res.data;
            if (data.pay_status == 2) {
              app.showModal(data.msg).then(() => {
                this.setData({
                  search: '',
                  page: 1,
                  flag: true,
                  list: [],
                })
                this.getList()
              })
              return
            }
            if (data.code == 0) {
              wx.showToast({
                title: data.msg,
                icon: 'none'
              })
              return
            }
            if (getApp().globalData.appletpayves_info.work == 3) {
              getApp().payPlugin(data.data.orderNo).then(() => {
                getApp().showModal('支付成功').then(() => {
                  this.setData({
                    search: '',
                    page: 1,
                    flag: true,
                    list: [],
                  })
                  this.getList()
                })
              })
              return
            }

            if (data.data.jump) {
              this.selectComponent('#zfb_pay').get_code(data.data.jump)
              this.setData({
                show_code_img: true,
                has_jump: true,
              })
              return
            } else {
              this.setData({
                has_jump: false
              })
            }

            if (!data.data.package) {
              getApp().showModal('暂无法使用该支付方式，请选择其他支付方式', true).then(() => {
                this.RenewWork(e)
              })
              return
            }

            wx.requestPayment({
              nonceStr: data.data.nonceStr,
              package: data.data.package,
              paySign: data.data.paySign,
              timeStamp: data.data.timeStamp,
              signType: data.data.signType,
              complete: (s) => {
                if ("requestPayment:ok" == s.errMsg) {
                  wx.showToast({
                    title: "支付成功",
                    icon: 'none'
                  })
                  this.setData({
                    search: '',
                    page: 1,
                    flag: true,
                    list: [],
                  })
                  this.getList()
                }
              }
            })

          })
        }
      })
    })
  },
  applyRefundWorkYjOrder(e) {
    if (e.currentTarget.dataset.refund > 0) {
      return
    }
    let key = 'ef4906b73a7bf35b5c586257fe732cd9';
    let time = new Date().getTime();
    getApp().globalData.posts('/api/ouyoung/order/applyRefundWorkYjOrder', {
      work_id: e.currentTarget.dataset.id,
    }).then(res => {
      getApp().showModal(res.data.msg).then(() => {
        this.setData({
          page: 1,
          list: []
        })
        this.getList()
      })
    })
  },
  to_qm(e) {
    wx.navigateTo({
      url: '/pages/aunt/handwriting/index?work_id=' + e.currentTarget.dataset.id,
    })
  },
  look_bd(e) {
    let id = e.currentTarget.dataset.id
    getApp().globalData.posts('/index.php/api/ouyoung/oyc/sorting/ouyapi/xb_policyDownload', {
      id
    }).then(res => {
      let data = res.data.data
      let fileBytes = data.fileBytes
      let fileName = data.fileName
      const filePath = wx.env.USER_DATA_PATH + '/' + fileName;
      const buffer = wx.base64ToArrayBuffer(fileBytes);
      const fsm = wx.getFileSystemManager();
      fsm.writeFile({
        filePath,
        data: buffer,
        encoding: 'binary',
        success() {
          wx.openDocument({
            filePath,
            showMenu: true
          })
        }
      });
    })
  },
  appraise(e) { //评价
    console.log(e)
    wx.navigateTo({
      url: './appraise?id=' + e.currentTarget.dataset.id + '&to_uid=' + e.currentTarget.dataset.to_uid + '&type='+e.currentTarget.dataset.type,
    })
  }
})